package com.liu.fresh.controller;


import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.liu.fresh.biz.IGoodsInfoBiz;
import com.liu.fresh.entity.GoodsInfo;
import com.liu.fresh.util.StringUtil;

@Controller
@RequestMapping("/goodsInfo")
public class GoodsInfoController  {
	@Autowired
	private IGoodsInfoBiz goodsInfoBiz;

	
	@RequestMapping("/findAll")
	@ResponseBody
	public List<GoodsInfo> findAll() {									
		return goodsInfoBiz.findAll();
	}
	
	@RequestMapping("/find")
	@ResponseBody
	public GoodsInfo find(String str) {
		System.out.println(goodsInfoBiz.find(str));
		return goodsInfoBiz.find(str);
	}
	
	@RequestMapping("/findByTno")
	@ResponseBody
	public List<GoodsInfo> findByTno(String tno,String start) {
		return goodsInfoBiz.findByTno(tno,start);
	}
	
	@RequestMapping("/updateBal")
	@ResponseBody
	public Integer updateBal(String[] gnos,String[] nums) {
		return goodsInfoBiz.updateBal(gnos,nums);
	}
	
	
	@RequestMapping("/finds")
	@ResponseBody
	public List<GoodsInfo> finds() {
		return goodsInfoBiz.finds();
	}
	@RequestMapping("/upload")
	@ResponseBody
	public Map<String, Object> add(@RequestParam("upload")MultipartFile pic,HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(pic.isEmpty()){
			return map;
		}
		try{
			String savePath = "images/goods";
			
			String path = request.getServletContext().getRealPath("");
			String temp = request.getServletContext().getInitParameter("uploadPath");
			if(temp != null){
				savePath = temp;
			}
			
			//在用户上传的文件名的前面加上时间戳
			savePath += "/" + new Date().getTime() + "_" +pic.getOriginalFilename();
			File dest = new File(new File(path).getParentFile(),savePath);
			
			//将本地图片保存到服务器
			pic.transferTo(dest);
			
			map.put("fileName", pic.getOriginalFilename());
			map.put("uploaded", 1);
			map.put("url","../../../"+savePath);
		}catch(IllegalStateException e){
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}
		return map;
		
	}
	//管理员端的商品信息
	@RequestMapping("/addGood")
	@ResponseBody
	public int addGood(@RequestParam Map<String,Object> map,@RequestParam MultipartFile pic, HttpServletRequest request){
		int result =-1;

		if(pic.isEmpty()){
			result=-2;//说明没有图片需要上传
		}
		String savePath="";
		try {
			String path= request.getServletContext().getRealPath("");
			String temp = request.getServletContext().getInitParameter("uploadpath");
			if(!StringUtil.checkNull(temp)){
				savePath = temp;
			}
			
			savePath="images/goods/"+pic.getOriginalFilename();

			File dest = new File(path, savePath);
			//将图片存到服务器的指定文件夹
			pic.transferTo(dest);

		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		map.put("pics", savePath);
		result=goodsInfoBiz.addGood(map);
		return result;
	}
	
	@RequestMapping("/findgoods")
	@ResponseBody
	public List<GoodsInfo> findgoods() {									
		return goodsInfoBiz.findgoods();
	}
	
	@RequestMapping("/del")
	@ResponseBody
	public int del(String gno) {			
		return goodsInfoBiz.del(gno);
	}
	
	@RequestMapping("/getPage")
	@ResponseBody
	public Integer getPage(String tno) {
		int total=goodsInfoBiz.getTotal(tno);
		int page=total%10==0?total/10:total/10+1;
		return page;
	}
}
